<template>
  <view class="cash-form">
    <view class="tip">
      <view class="title">使用说明:</view>
      <view class="text">1, 积分支付时使用</view>
      <view class="text">2, 提现时使用</view>
    </view>
    <u-line></u-line>
    <u--form
        labelPosition="left"
        :model="postForm"
        :rules="rules"
        ref="uForm"
        class="form"
        labelWidth="160rpx"
        labelAlign="right"
        :labelStyle="{'font-size': '24rpx', 'padding-right': '20rpx', 'margin-top': '-3rpx'}"
    >
      <u-form-item label="旧密码:" prop="oldPassword" v-if="postForm.isExist">
        <u--input
            v-model="postForm.oldPassword"
            type="password"
            border="surround"
            fontSize="24rpx"
            :customStyle="{'height': '30rpx'}"
        ></u--input>
      </u-form-item>
      <u-form-item label="新密码:" prop="password1">
        <u--input
            v-model="postForm.password1"
            type="password"
            border="surround"
            fontSize="24rpx"
            :customStyle="{'height': '30rpx'}"
        ></u--input>
      </u-form-item>
      <u-form-item label="重复密码:" prop="password2">
        <u--input
            v-model="postForm.password2"
            type="password"
            border="surround"
            fontSize="24rpx"
            :customStyle="{'height': '30rpx'}"
            color="red"
        ></u--input>
      </u-form-item>
      <u-button type="success" class="btn" :color="activeColor" @click="save" text="保存"></u-button>
    </u--form>
    <u-toast ref="uToast"></u-toast>
    <view class="forget" @click="goForget">忘记支付密码,重置密码</view>
  </view>
</template>

<script>
import color from '@/config/color.js'
import base64 from '../../util/base64'
import User from '../../api/user'
const userModel = new User()
export default {
  data() {
    return {
      activeColor: color.main_color,
      rules: {
        'oldPassword': {
          type: 'string',
          required: true,
          message: '请输入旧密码',
          trigger: ['blur', 'change']
        },
        'password1': {
          type: 'string',
          required: true,
          message: '请输入新密码',
          trigger: ['blur', 'change']
        },
        'password2': {
          type: 'string',
          required: true,
          message: '请输入重复密码',
          trigger: ['blur', 'change']
        }
      },
      postForm: {
        isExist: false,
        oldPassword: '',
        password1: '',
        password2: '',
      }
    }
  },
  mounted() {
    this.fetch()
  },
  methods: {
    goForget() {
      uni.navigateTo({
        url: '/sub_service/pay-forget/index'
      })
    },
    fetch() {
      userModel.checkPayPassword().then(response => {
        let { data } = response
        data = base64.parse(data)
        console.log(data)
        this.postForm.isExist = data.result
      })
    },
    save() {
      this.$refs.uForm.validate().then(res => {
        console.log(res)
        if (res) {
          if (this.postForm.password1 !== this.postForm.password2) {
            return uni.showToast({
              icon: 'none',
              title: '两次输入密码不同!'
            })
          }
          userModel.editPayPassword({
            ...this.postForm,
            old_password: this.postForm.oldPassword
          }).then(() => {
            this.$refs.uToast.show({
              type: 'success',
              message: '操作成功',
              duration: 800,
              iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
              complete: () => {
                uni.navigateBack()
              }
            })
          })
        }
      }).catch(e => {
      })

    }
  }
}
</script>

<style scoped lang="scss">
.forget {
  color: $main_color;
  text-align: center;
  padding: 20px 0;
}
.cash-form {
  padding: 20px 10px;
  .tip {
    font-size: 24rpx;
    margin-bottom: 10px;
    view {
      height: 20px;
    }
    .title {
      font-weight: 700;
    }
  }
  .form {
    padding-top: 10px;
    .btn {
      margin-top: 10px;
    }
  }
}
</style>
